 #include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 2 

Servo myservo;  

int ledPin = 7; // 设置LED连接到Arduino板子上的pin 13端口
int sensor = 3; //设置传感器信号输出端接到Arduino板子上的Pin2端口
int val1 = 0;


byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;
char server[] = "1.cchome.applinzi.com";
char light1='2'; //庭院灯  pin6 端口控制
char light2='2'; //灯光总控制 pin3 庭院灯 pin4 Redled pin5 red
char light3='2'; //室内灯 pin11 门灯
char light4='2'; //车库门控制 pin9 pin5 车库状态灯
char light5='2'; //布防模块 pin
char light6='2';//休眠模式，所有设备全部关闭
char c;
int potpin = 0;  //该变量用于存储用电位器读出的模拟值
int val; 
//定义数组处理返回值、
int list[10];
byte i=9; 
char tec;
 

unsigned long lastConnectionTime = 0;          // 以毫秒为单位，最后一次连接服务器时间
boolean lastConnected = false;                 // 上一次连接状态
const unsigned long postingInterval = 10;  // 两次连接的时间间隔
void setup()
{
  myservo.attach(9);  //车库门端口初始化
  pinMode(5, OUTPUT);     //LED灯接入数字5口 LED车库灯
  pinMode(6, OUTPUT);     //LED灯接入数字6口 庭院灯//
  pinMode(11, OUTPUT);     //LED灯接入数字11口 门灯/
  pinMode(4, OUTPUT);     //LED灯接入数字4口 Redled
  pinMode(ledPin, OUTPUT); // 设置为输出数字管脚
  pinMode(sensor, INPUT); // 设置为输入数字管脚
  Serial.begin(9600);
  Ethernet.begin(mac);
  turnclose();//如果为固定IP方式为Ethernet.begin(mac, ip)
}
void loop(void)
{ Serial.println("\n");
 
  int chk = DHT11.read(DHT11PIN);
 
    Serial.println((float)DHT11.temperature, 1);
    tec=((char)DHT11.temperature);
    Serial.println(tec);
    
    while(client.available()) 
    {                 //调试
        c = client.read();
        Serial.print(c);
        if (c == '*')
       {
            light1= client.read();
            light2= client.read();
            light3= client.read();
            light4= client.read();
            light5= client.read();
            light6= client.read();
            Serial.println(light1);
            Serial.print("swich1:"); 
            Serial.println(light1);
            Serial.print("swich2:"); 
            Serial.println(light2); 
            Serial.print("swich3:");   
            Serial.println(light3); 
            Serial.print("swich4:");   
            Serial.println(light4);
            Serial.print("swich5:");   
            Serial.println(light5);
            Serial.print("swich6:");   
            Serial.println(light6);           
        }
       
    }
   //Serial.println(list[0]);
  //*******************庭院灯*******************
 if(light1 == '2')
    {             //控制LED灯开关
        digitalWrite(6, LOW);      
    }
    if(light1 == '1')
    {
      digitalWrite(6, HIGH);
    }
//***************灯光总控制*********************


//********************门灯控制******************
if(light3 == '2')
    {             //控制LED灯开关
        digitalWrite(11, LOW); 
            
    }
    if(light3 == '1')
    {
      digitalWrite(11, HIGH);
     
    }


//********************安防控制******************
if(light5 == '2')
    {        
              
    }
    if(light5 == '1')
    {
      val1 = digitalRead(sensor); // 读取输入引脚
      Serial.println(val1);
      if(HIGH == val1)
      digitalWrite(ledPin,HIGH); // 设定LED的高电平
      else
      digitalWrite(ledPin,LOW); // 设定LED的低电平
    }

//*******车库门门关联灯光控制 pin5**************
  
    if(light4 == '2')
    {             //控制LED灯开关
        digitalWrite(5, LOW); 
        turnclose();     
    }
    if(light4 == '1')
    {
      turnopen();
        digitalWrite(5, HIGH);
    }
//休眠模式*********************
  







     
    if (!client.connected() && lastConnected) 
    { //断开连接
        Serial.println("disconnecting.");
        client.stop();
    }
 
    if(!client.connected() && (millis()  > postingInterval)) 
    {    //重新连接
    Serial.println("read data ");
    getData();              //上传数据
    delay(1000);
    }
    lastConnected = client.connected();
}

/********************获取命令回传数值子函数********************/
void getData(void) {
    if (client.connect(server,80)) 
    {
        Serial.println("connecting...");
        client.println("GET /upload.php?token=doubleq&data=c HTTP/1.1");
        client.println("Host: 1.cchome.applinzi.com");
        client.println("User-Agent: arduino-ethernet");
        client.println("Connection: close");
        client.println();

        client.print("GET /tem.php?daca=");
        client.print(i);
        client.println(" HTTP/1.1");
        client.println("Host: 1.cchome.applinzi.com");
        client.println("User-Agent: arduino-ethernet");
        client.println("Connection: close");
        client.println();
        lastConnectionTime = millis();//系统已运行时间
    }else {
        Serial.println("connection failed");
        Serial.println("disconnecting.");
        client.stop();
    }
}
/********************舵机控制子函数********************/
void turnopen() //车库门开启
{ 
  val = analogRead(potpin);            //读取电位器控制的模拟值 (范围在0-1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(0);                  
// 指定舵机转向的角度  
  delay(15);                           
// 等待15ms让舵机到达指定位置   
}
void turnclose() 
{ 
  val = analogRead(potpin);            //读取电位器控制的模拟值 (范围在0-1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(75);                  
// 指定舵机转向的角度  
  delay(15);                           
// 等待15ms让舵机到达指定位置   
}

double Fahrenheit(double celsius) 
{
        return 1.8 * celsius + 32;
}    //摄氏温度度转化为华氏温度
 
double Kelvin(double celsius)
{
        return celsius + 273.15;
}     //摄氏温度转化为开氏温度
 
// 露点（点在此温度时，空气饱和并产生露珠）
// 参考: [url=http://wahiduddin.net/calc/density_algorithms.htm]http://wahiduddin.net/calc/density_algorithms.htm[/url] 
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}
 
// 参考: [url=http://en.wikipedia.org/wiki/Dew_point]http://en.wikipedia.org/wiki/Dew_point[/url]
double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}
 

 

 




